﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using W.Purchase.API.Application.Command.ProcurementCommand;

namespace W.Purchase.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProcurementController : ControllerBase
    {
        private readonly IMediator _mediator;
        public ProcurementController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 添加采购信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddProcurement(ProcurementCreateCommand command)
        {
            var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
            var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
            var name = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Name)?.Value;//获取名称
            command.CreateName = name;
            command.user_id = Convert.ToInt32(uid);
            command.office_id = sid;
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 修改采购信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateProcurement(ProcurementUpdateCommand command)
        {
            var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
            var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
            var name = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Name)?.Value;//获取名称
            command.UpdateName = name;
            command.user_id = Convert.ToInt32(uid);
            command.office_id = sid;
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 删除采购
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteProcurement(ProcurementDeleteCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 批量删除采购
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> RangeDeleteProcurement(ProcurementDeleteRangeCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 添加退货管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddReturn(ReturnCreateCommand command)
        {
            var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
            var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
            var name = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Name)?.Value;//获取名称
            //command.UpdateName = name;
            //command.user_id = Convert.ToInt32(uid);
            //command.office_id = sid;
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 删除采购退货
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteReturn(ReturnDelectCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 批量删除采购退货
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteRangeReturn(ReturnDelectRangeCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 采购入账
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ProcurementRecorded(ProcurementRecordedCommand command)
        {
            return await _mediator.Send(command);
        }
    }
}
